//显示图示和评价
var showDrvRankComment;
require(['grid', 'topBar', 'date', 'validator', 'highcharts-more'], function () {
    //提交到后台参数
    var postData;
    $('#top').topBar({
        left: [
            {
                type: 'filter',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name: 'reportType',
                        targetId: 'beginTime',
                        type: 'reportDateSelect'
                    }, {
                        text: '查询时间',
                        id: 'beginTime',
                        name: 'beginTime',
                        type: 'text',
                        width: 250
                    }
                ]
            }
        ],
        right: [{
            text: '导出',
            type: 'export-btn',
            authority: '',
            item: [{
                text: 'Excel导出',
                click: function () {
                    exportFile('Excel');
                }
            }, {
                text: 'PDF导出',
                click: function () {
                    exportFile('Pdf');
                }
            }]
        }]
    });

    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") < 1) {
            $.messager.alert("提示", "请先查询数据。", "info");
            return;
        }
        if (getPostData()) {
            var url = "/driverRank/exportDriverRank" + suffix + ".htm";
            $.exportFile(url, postData);
        }
    }

    $('#grid').parent().height($(window).height() - 174);

    $('#grid').grid({
        multiselect: false,
        url: "/driverRank/searchDriverRank.htm",
        colModel: [
            {name: "allRank", label: "排名", align: "right", width: 40},
            {
                name: "totalGrade",
                label: "整体得分",
                sortable: false,
                align: "right",
                formatter: "customCurrency",
                width: 80
            },
            {name: "drvName", label: "驾驶员", width: 80},
            {name: "organName", label: "所属机构", width: 80},
            {name: "lineName", label: "线路号", width: 80},
            {name: "totalMile", label: "运行里程(KM)", align: "right", formatter: "customCurrency", width: 120},
            {name: "lastingTime", label: "运行时长", align: "left", width: 80},
            {name: "moodGrade", label: "情绪得分", align: "right", formatter: "customCurrency", width: 80},
            {name: "safeGrade", label: "安全得分", align: "right", formatter: "customCurrency", width: 80},
            {name: "securityGrade", label: "安保得分", align: "right", formatter: "customCurrency", width: 80},
            {name: "energyGrade", label: "节能得分", align: "right", formatter: "customCurrency", width: 80}],
        onSelectRow: function (ids) {
            var data = $('#grid').getRowData(ids);
            showDrvRankComment(data);
        }
    });

    //显示图示和评价
    showDrvRankComment = function (data) {
        $("#drvRankName").html(data.drvName);
        $("#totalScore").html(data.totalGrade);
        $("#drvRank").html(data.allRank);
        $("#driverRankCharts").highcharts({
            chart: {
                polar: true
            },
            title: {
                text: '总评分蛛网图',
                x: -80
            },
            pane: {
                size: '80%'
            },
            credits: {
                enabled: false
            },
            xAxis: {
                categories: ["情绪得分", "安全得分", "安保得分", "节能得分"],
                tickmarkPlacement: 'on',
                lineWidth: 0
            },
            yAxis: {
                gridLineInterpolation: 'polygon',
                lineWidth: 0,
                min: 0,
                minorTickInterval: 'auto'
            },
            legend: {
                align: 'right',
                verticalAlign: 'top',
                y: 70,
                layout: 'vertical'
            },
            series: [{
                name: '各项分数',
                data: [parseInt(data.moodGrade), parseInt(data.safeGrade), parseInt(data.securityGrade), parseInt(data.energyGrade)],
                pointPlacement: 'on'
            }]
        });
        $("#showDriverRankMsg").css("display", "block");
    };

    function search() {
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    }

    function getPostData() {
        postData = $('#top').parent().getValues();
        if (!postData.beginTime) {
            $.messager.alert("提示", "请先选择查询时间。", "info");
            return false;
        }
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.LINE) {
            postData.orgIds = nodes.LINE;
            postData.type = "LINE";
            return true;
        }
        $.messager.alert("提示", "至少请选择一条线路。", "info");
        return false;
    }

});
